package com.kefu.robot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.annotation.Permission;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.robot.model.RobotInfo;
import com.kefu.robot.model.RobotUnknown;
import com.kefu.robot.service.RobotInfoService;
import com.kefu.robot.service.RobotUnknownService;
import com.kefu.robot.vo.RobotUnknownSearchVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags="未知问题")
@RestController
@RequestMapping("robot/unknown")
public class RobotUnknownController extends AbstractBaseController{

    @Autowired
    private RobotUnknownService robotUnknownService;
    @Autowired
    private RobotInfoService robotInfoService;


    @PostMapping("/ignore")
    @ApiOperation(value="未知问题忽略", notes="未知问题忽略")
    public Result<String> ignore(@Validated @RequestBody DeleteVo delete) {
    	logger.info("未知问题忽略:{}",delete);
    	
        robotUnknownService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/list")
    @ApiOperation(value="查询未知问题", notes="查询未知问题，聊天记录调用在线客服接口:chat/agent/getSessionMsg")
    public Result<Page<RobotUnknown>> list(@Validated @RequestBody RobotUnknownSearchVo search) {    
        logger.info("查询未知问题:{}",search);
        
        Page<RobotUnknown> list = robotUnknownService.search(search);
        List<RobotInfo> robotList = robotInfoService.list(Wrappers.lambdaQuery(RobotInfo.class).eq(RobotInfo::getTenantId, KefuContext.getKefuContext().getTenantId()));
        list.getRecords().forEach(study -> {
            robotList.stream().filter(robot -> robot.getRobotNo().equals(study.getRobotNo()))
                    .findAny().ifPresent(robot -> study.setRobotName(robot.getRobotName()));
        });
        return ResultGenerator.genSuccessResult(list);
    }

}
